//package org.howie.config;
//
//import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
//import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
//import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
//import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
//import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
//import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
//import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
//import com.hs.springcloud.domain.Result;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.web.reactive.function.BodyInserters;
//import org.springframework.web.reactive.function.server.ServerResponse;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import javax.annotation.PostConstruct;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @Description: 自定义异常处理
// * @Author 胡尚
// * @Date: 2022/7/9 17:50
// * @Version 1.0
// */
//@Configuration
//public class GatewayConfig {
//
//    @PostConstruct
//    public void init(){
//
//        // 1. 创建一个BlockRequestHandler对象，它是一个接口，所以就需要匿名内部类
//        BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
//            @Override
//            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
//                // 定义一个返回对象，这是自定义的一个对象
//                Result result = null;
//
//                if (throwable instanceof FlowException){
//                    result = Result.error(500, "接口限流了");
//                }else if (throwable instanceof DegradeException){
//                    result = Result.error(500, "服务降级了");
//                }else if (throwable instanceof SystemBlockException){
//                    result = Result.error(500, "触发系统保护规则了");
//                }
//
//                // 返回数据
//                return ServerResponse.status(HttpStatus.OK) // 响应状态码
//                        .contentType(MediaType.APPLICATION_JSON)  // 响应数据类型，这里设置为json
//                        .body(BodyInserters.fromValue(result)); // 响应数据
//            }
//        };
//
//        // 2.通过调用GatewayCallbackManager对象中的setBlockHandler()方法，将我们上次创建的对象设置进去
//        GatewayCallbackManager.setBlockHandler(blockRequestHandler);
//    }
//}
//
